

#include "defeatlayer.h"
#include "gamescene.h"
#include "resourcemanager.h"
#include "log.h"
#include "gamemanager.h"
#include "cocos-ext.h"
#include "gamedatamanager.h"
#include "messagequeue.h"
#include "campaignscene.h"
#include "armiesscene.h"
#include "battlescene.h"

DefeatLayer::DefeatLayer()
{

}

DefeatLayer::~DefeatLayer() {

}

bool DefeatLayer::init() {
	if (!CCLayer::init()) {
		return false;
	}

	cocos2d::extension::CCScale9Sprite *sprFrame = cocos2d::extension::CCScale9Sprite::create("textures/frame.png", CCRectMake(0, 0, 96, 96), CCRectMake(32, 32, 32, 32));
	sprFrame->setContentSize(CCSizeMake(725, 320));
	sprFrame->setPosition(XY_SCALE(480, 288));
	sprFrame->setScale(GM->minscale);
	sprFrame->setAnchorPoint(ccp(0.5f, 0.5f));
	addChild(sprFrame);

	cocos2d::extension::CCScale9Sprite *sprBg = cocos2d::extension::CCScale9Sprite::create("textures/pad_bg.png", CCRectMake(0, 0, 225, 225), CCRectMake(100, 100, 25, 25));
	sprBg->setContentSize(CCSizeMake(700, 290));
	sprBg->setScale(GM->minscale);
	sprBg->setPosition(XY_SCALE(480, 288));
	sprBg->setAnchorPoint(ccp(0.5f, 0.5f));
	addChild(sprBg);

	CCSprite* sprTitle = MAKE_SPRITE("defeat_header.png");
	sprTitle->setPosition(ccp(350, 285));
	sprTitle->setScale(1.0f);
	sprBg->addChild(sprTitle);

	auto btnClose = RM->makeButton("button/blue_button_0.png", "button/blue_button_1.png", nullptr, this, menu_selector(DefeatLayer::onClose), sprBg);
	btnClose->setPosition(ccp(350, 50));
	RM->makeButtonLabel("OK", 24, btnClose);

	return true;
}

void DefeatLayer::onClose(CCObject *sender) {
	CCScene* scene = nullptr;
	switch(targetScene) {
		case MessageTarget::campaign_scene:
			scene = CampaignScene::create();
			break;
		case MessageTarget::online_battle_scene:
			scene = BattleScene::create();
	        break;
		default:
			scene = ArmiesScene::create();
			break;
	}

	CCDirector::sharedDirector()->replaceScene(scene);
}

void DefeatLayer::keyBackClicked() {
	onClose(nullptr);
}

void DefeatLayer::keyMenuClicked() {

}
